import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ZhongJinTao
 * Date: 2024-01-06
 * Time: 9:17
 */
public class MyStack implements IStack{

    private int[] elem;
    private int usedSize;
    private int DEFAULT_CAPACITY = 10;

    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }
    public MyStack(int n) {
        this.elem = new int[n];
    }

    @Override
    public void push(int x) {
        if(full()) {
            //扩容
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
        elem[usedSize++] = x;
    }
    private boolean full() {
        return usedSize == elem.length;
    }

    @Override
    public int pop() {
        if(usedSize == 0) {
            throw new EmptyException("栈空了");
        }
        int tmp = elem[usedSize - 1];
        usedSize--;
        return tmp;
    }

    @Override
    public int peek() {
        if(usedSize == 0) {
            throw new EmptyException("栈空了");
        }
        int tmp = elem[usedSize - 1];
        return tmp;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public boolean empty() {
        return usedSize == 0;
    }
}
